package com.youmans.edumember.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youmans.commonutils.JwtUtils;
import com.youmans.commonutils.MD5;
import com.youmans.edumember.entity.UcenterMember;
import com.youmans.edumember.entity.vo.LoginVo;
import com.youmans.edumember.entity.vo.RegisterVo;
import com.youmans.edumember.mapper.UcenterMemberMapper;
import com.youmans.edumember.service.UcenterMemberService;
import com.youmans.servicebase.exceptionHandler.GuliException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Youmans
 * @since 2022-05-17
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate ;
    private int i = 0 ;
    @Override
    public String login(LoginVo member) {
        String mobile = member.getMobile();
        String password = member.getPassword();
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"登录失败，手机号或密码错误") ;
        }
//        判断查询对象是否为空
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>() ;
        wrapper.eq("mobile",mobile) ;
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);


        if(ucenterMember == null){
            throw new GuliException(20001,"登录失败,手机号不存在") ;
        }
        if(!MD5.encrypt(password).equals(ucenterMember.getPassword())){
            throw new GuliException(20001,"登录失败，手机号或密码错误") ;
        }
        ucenterMember.setPassword(null) ;
        if(ucenterMember.getIsDeleted()){
            throw new GuliException(20001,"登录失败，账号禁用，请联系管理员") ;
        }

//        登录成功，生成token字符串，使用jwt工具
        return  JwtUtils.getJwtToken(ucenterMember.getId(),ucenterMember.getNickname()) ;
    }

//    注册
    @Override
    public void register(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        if(StringUtils.isEmpty(code)){
            throw new GuliException(20001,"注册失败，验证码错误") ;
        }

        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(nickname)||StringUtils.isEmpty(password)){
            throw new GuliException(20001,"注册失败，空字符") ;
        }

        String redisCode = redisTemplate.opsForValue().get("guli_school:code:" + mobile);

        if(!code.equals(redisCode)){
            throw new GuliException(20001,"注册失败，验证码错误") ;
        }
        //验证码一次性确认删除
        redisTemplate.delete("guli_school:code:" + mobile);

        //验证码3次正确，删除验证码
//        while(i>1){
//            redisTemplate.delete("guli_school:code:" + mobile);
//            i=0 ;
//        }
//        i+=1 ;

//        判断手机号重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>() ;
        wrapper.eq("mobile",mobile) ;
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0){
            throw new GuliException(20001,"该手机号已经注册请重新输入") ;
        }

        UcenterMember member = new UcenterMember() ;
        BeanUtils.copyProperties(registerVo,member);
        member.setPassword(MD5.encrypt(password));
        member.setIsDeleted(false);
        member.setAvatar("https://guli-school-online.oss-cn-hangzhou.aliyuncs.com/2022-05-15/ee46abaa5ccb48e59d837d7f6de430de.png") ;
        baseMapper.insert(member) ;
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>() ;
        wrapper.eq("openid",openid) ;
        return  baseMapper.selectOne(wrapper);
    }

    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegister(day);
    }
}
